package com.barcode;
import java.security.PrivateKey;
import com.baractivity.CaptureActivity;
import com.rsa.RsaHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BarCodeActivity extends Activity {
   
	private static TextView resultTextView;
	private static EditText edtDecKey;
	String strprivatekey =
			"<RSAKeyValue><Modulus>6CdsXgYOyya/yQHTO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5LwPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sxPAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sPw9YRXiac=</Modulus><Exponent>AQAB</Exponent><P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4dL411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRdVgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOleVTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpGlq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q><DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FUGrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Tezc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArYgw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjAStUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjncWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2DCr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZs7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQryoHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";

	PrivateKey privateKey = RsaHelper.decodePrivateKeyFromXml(strprivatekey);
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.barcode);                
        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
		//打开扫描界面扫描条形码或二维码
        Button btnscan = (Button) this.findViewById(R.id.btn_scan_open);

        btnscan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Intent openCameraIntent = new Intent(BarCodeActivity.this,CaptureActivity.class);
				startActivityForResult(openCameraIntent, 0);
			}
		});
      
        //长按复制
        resultTextView.setOnLongClickListener(new OnLongClickListener() {
        	@Override
        	public boolean onLongClick(View arg0) {
        		copy();		
        		return false;
       		}
	
		});
    }

	public Dialog copy() {
		Dialog dialog = new AlertDialog.Builder(this).setTitle("提示").setNegativeButton("取消", null).
				setItems(new String[] {"复制" },
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String string = edtDecKey.getText().toString();
						copy(string, getBaseContext());
						Toast.makeText(getBaseContext(), "文本已复制到粘贴板", Toast.LENGTH_LONG);
						}
					}).create();
		
			dialog.setCanceledOnTouchOutside(false);
			dialog.show();
			return dialog;
		}
		//复制
		
		public static void copy(String content, Context context) {		
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(content.trim());		
		}

		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			super.onActivityResult(requestCode, resultCode, data);
			//处理扫描结果（在界面上显示）
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				String scanResult = bundle.getString("result");
				resultTextView.setText(scanResult);					              
		}
	}
	
}


			
